package com.example.log.alarm.advice.logger;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.example.log.alarm.spy.QueuedSpy;
import net.bytebuddy.asm.Advice;

/**
 * @program: spring-agent
 * @description:
 * @author: zt22328 赵儒定
 * @create: 2023-05-05 15:49
 **/
public class LoggerAdviceInterceptor {

    /**
     * ch.qos.logback.classic.Logger
     * public void callAppenders(ILoggingEvent event)
     * @param args
     */
    @Advice.OnMethodEnter
    public static void enter(@Advice.AllArguments Object[] args) {
        if (args == null || !(args[0] instanceof ILoggingEvent)) {
            return;
        }
        ILoggingEvent event = (ILoggingEvent) args[0];
        Level level = event.getLevel();
        if (level.levelInt < Level.WARN_INT) {
            return;
        }
        try {
            QueuedSpy.offer(event);
        } catch (Exception e) {
            System.out.println("ILoggingEvent：" + e.getMessage());
        }
    }
}
